package org.light.generator;

import java.util.ArrayList;
import java.util.List;

import org.javaforever.gatescore.utils.StringUtil;
import org.light.core.Writeable;
import org.light.domain.Statement;
import org.light.domain.Util;
import org.light.utils.WriteableUtil;

public class EncrptUtilGenerator extends Util{
	public EncrptUtilGenerator(){
		super();
		super.fileName = "EncrptUtil.go";
	}
	
	public EncrptUtilGenerator(String packageToken){
		super();
		this.setPackageToken(packageToken);
		super.fileName = "EncrptUtil.go";
	}
	
	public String getPackageTokenWithDot() {
		if (StringUtil.isBlank(this.getPackageToken())) return "";
		else return this.getPackageToken()+".";
	}
	
	@Override
	public void setPackageToken(String packageToken) {
		this.packageToken = packageToken;
	}
	
	@Override
	public String generateUtilString() {
		List<Writeable> sList = new ArrayList<Writeable>();
		sList.add(new Statement(1000L,0,"package utils"));
		sList.add(new Statement(2000L,0,""));
		
		sList.add(new Statement(2100L,0,"import ("));
		sList.add(new Statement(2200L,1,"\"crypto/md5\""));
		sList.add(new Statement(2300L,1,"\"crypto/sha1\""));
		sList.add(new Statement(2400L,1,"\"fmt\""));
		sList.add(new Statement(2500L,1,"\"io\""));
		sList.add(new Statement(2600L,1,"\"encoding/hex\""));
		sList.add(new Statement(2700L,0,")"));
		
		sList.add(new Statement(3000L,0,"func Sha1Hash(data string) string {"));
		sList.add(new Statement(4000L,1,"t := sha1.New()"));
		sList.add(new Statement(5000L,1,"io.WriteString(t, data)"));
		sList.add(new Statement(6000L,1,"return fmt.Sprintf(\"%x\", t.Sum(nil))"));
		sList.add(new Statement(7000L,0,"}"));
		
		sList.add(new Statement(8000L,0,"func Md5Hash(data string) string {"));
		sList.add(new Statement(9000L,1,"t := md5.New()"));
		sList.add(new Statement(10000L,1,"io.WriteString(t, data)"));
		sList.add(new Statement(11000L,1,"return fmt.Sprintf(\"%x\", t.Sum(nil))"));
		sList.add(new Statement(12000L,0,"}"));
		
		sList.add(new Statement(40000L,0,""));
		sList.add(new Statement(41000L,0,"func MultiSha1(password, salt string, iter int) string{"));
		sList.add(new Statement(42000L,1,"b := []byte(password)"));
		sList.add(new Statement(43000L,1,"s := []byte(salt)"));
		sList.add(new Statement(44000L,1,"h := sha1.New()"));
		sList.add(new Statement(45000L,1,"h.Write(s) // 先传入盐值，之前因为顺序错了卡了很久"));
		sList.add(new Statement(46000L,1,"h.Write(b)"));
		sList.add(new Statement(47000L,1,"var res []byte"));
		sList.add(new Statement(48000L,1,"res = h.Sum(nil)"));
		sList.add(new Statement(49000L,1,"for i := 0; i < iter-1; i++ {"));
		sList.add(new Statement(50000L,2,"h.Reset()"));
		sList.add(new Statement(51000L,2,"h.Write(res)"));
		sList.add(new Statement(52000L,2,"res = h.Sum(nil)"));
		sList.add(new Statement(53000L,1,"}"));
		sList.add(new Statement(54000L,1,"return hex.EncodeToString(res)"));
		sList.add(new Statement(55000L,0,"}"));
		sList.add(new Statement(56000L,0,""));
		sList.add(new Statement(57000L,0,"func MultiMD5(str string, salt string, iteration int) string {"));
		sList.add(new Statement(58000L,1,"b := []byte(str)"));
		sList.add(new Statement(59000L,1,"s := []byte(salt)"));
		sList.add(new Statement(60000L,1,"h := md5.New()"));
		sList.add(new Statement(61000L,1,"h.Write(s) // 先传入盐值，之前因为顺序错了卡了很久"));
		sList.add(new Statement(62000L,1,"h.Write(b)"));
		sList.add(new Statement(63000L,1,"var res []byte"));
		sList.add(new Statement(64000L,1,"res = h.Sum(nil)"));
		sList.add(new Statement(65000L,1,"for i := 0; i < iteration-1; i++ {"));
		sList.add(new Statement(66000L,2,"h.Reset()"));
		sList.add(new Statement(67000L,2,"h.Write(res)"));
		sList.add(new Statement(68000L,2,"res = h.Sum(nil)"));
		sList.add(new Statement(69000L,1,"}"));
		sList.add(new Statement(70000L,1,"return hex.EncodeToString(res)"));
		sList.add(new Statement(71000L,0,"}"));

		return WriteableUtil.merge(sList).getContent();
	}

}
